package com.demo.dao;
import com.demo.bean.User;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDao {

  // 查询所有用户
  public List<User> selectAllUsers(Connection conn) throws SQLException {
    List<User> users = new ArrayList<>();
    String sql = "SELECT * FROM user";
    try (PreparedStatement stmt = conn.prepareStatement(sql);
        ResultSet rs = stmt.executeQuery()) {
      while (rs.next()) {
        users.add(mapResultSetToUser(rs));
      }
    }
    return users;
  }

  // 根据用户ID查询用户
  public User selectUserByUserId(Connection conn, String userId) throws SQLException {
    String sql = "SELECT * FROM user WHERE user_id = ?";
    try (PreparedStatement stmt = conn.prepareStatement(sql)) {
      stmt.setString(1, userId);
      try (ResultSet rs = stmt.executeQuery()) {
        if (rs.next()) {
          return mapResultSetToUser(rs);
        }
      }
    }
    return null;
  }

  // 新增用户
  public int insertUser(Connection conn, User user) throws SQLException {
    String sql = "INSERT INTO user (" +
        "user_id, avatar, user_name, password, " +
        "create_time, update_time, is_admin, status, silence" +
        ") VALUES (?, ?, ?, ?, NOW(), NOW(), ?, ?, ?)";

    try (PreparedStatement stmt = conn.prepareStatement(sql)) {
      stmt.setString(1, user.getUserId());
      stmt.setString(2, user.getAvatar());
      stmt.setString(3, user.getUserName());
      stmt.setString(4, user.getPassword());
      stmt.setString(5, user.getIsAdmin());
      stmt.setString(6, user.getStatus());
      stmt.setString(7, user.getSilence());

      return stmt.executeUpdate();
    }
  }

  // 根据用户ID删除用户
  public int deleteUserById(Connection conn, String userId) throws SQLException {
    String sql = "DELETE FROM user WHERE user_id = ?";
    try (PreparedStatement stmt = conn.prepareStatement(sql)) {
      stmt.setString(1, userId);
      return stmt.executeUpdate();
    }
  }

  // 更新用户信息
  public int updateUser(Connection conn, User user) throws SQLException {
    String sql = "UPDATE user SET " +
        "avatar = ?, " +
        "user_name = ?, " +
        "password = ?, " +
        "update_time = NOW(), " +
        "is_admin = ?, " +
        "status = ?, " +
        "silence = ? " +
        "WHERE user_id = ?";

    try (PreparedStatement stmt = conn.prepareStatement(sql)) {
      stmt.setString(1, user.getAvatar());
      stmt.setString(2, user.getUserName());
      stmt.setString(3, user.getPassword());
      stmt.setString(4, user.getIsAdmin());
      stmt.setString(5, user.getStatus());
      stmt.setString(6, user.getSilence());
      stmt.setString(7, user.getUserId());

      return stmt.executeUpdate();
    }
  }

  // 清理不活跃用户
  public int deleteInactiveUsers(Connection conn, int inactiveDays) throws SQLException {
    String sql = "DELETE FROM user WHERE status = 'N' AND create_time < DATE_SUB(NOW(), INTERVAL ? DAY)";
    try (PreparedStatement stmt = conn.prepareStatement(sql)) {
      stmt.setInt(1, inactiveDays);
      return stmt.executeUpdate();
    }
  }

  // 辅助方法：将ResultSet映射到User对象
  private User mapResultSetToUser(ResultSet rs) throws SQLException {
    User user = new User();
    user.setUserId(rs.getString("user_id"));
    user.setUserName(rs.getString("user_name"));
    user.setAvatar(rs.getString("avatar"));
    user.setPassword(rs.getString("password"));
    user.setCreateTime(rs.getTimestamp("create_time"));
    user.setUpdateTime(rs.getTimestamp("update_time"));
    user.setIsAdmin(rs.getString("is_admin"));
    user.setStatus(rs.getString("status"));
    user.setSilence(rs.getString("silence"));
    return user;
  }
}